package com.hogae.capital.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

import com.hogae.capital.domain.CapAccountRecord;
import com.hogae.capital.domain.CapPoolAccount;
import com.hogae.capital.service.ICapPoolAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Select;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hogae.core.annotation.Log;
import com.hogae.core.core.controller.BaseController;
import com.hogae.core.core.domain.AjaxResult;
import com.hogae.core.enums.BusinessType;
import com.hogae.core.utils.poi.ExcelUtil;

import com.hogae.capital.domain.CapPoolAccountRecord;
import com.hogae.capital.service.ICapPoolAccountRecordService;
import com.hogae.core.core.page.TableDataInfo;

/**
 * 资金池记录Controller
 *
 * @author hogae
 * @date 2021-02-26
 */
@Api(tags = "资金池记录")
@RestController
@RequestMapping("/capital/poolAccountRecord")
public class CapPoolAccountRecordController extends BaseController {
    @Autowired
    private ICapPoolAccountRecordService capPoolAccountRecordService;

    /**
     * 查询资金池记录列表
     */
    @ApiOperation("查询资金池记录列表")
    @PreAuthorize("@ss.hasPermi('capital:poolAccountRecord:list')")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public TableDataInfo list(CapPoolAccountRecord capPoolAccountRecord) {
        startPage();
        List<CapPoolAccountRecord> list = capPoolAccountRecordService.selectCapPoolAccountRecordList(capPoolAccountRecord);
        return getDataTable(list);
    }

    /**
     * 导出资金池记录列表
     */
    @ApiOperation("导出资金池记录列表")
    @PreAuthorize("@ss.hasPermi('capital:poolAccountRecord:export')")
    @Log(title = "资金池记录", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult export(CapPoolAccountRecord capPoolAccountRecord) {
        List<CapPoolAccountRecord> list = capPoolAccountRecordService.selectCapPoolAccountRecordList(capPoolAccountRecord);
        ExcelUtil<CapPoolAccountRecord> util = new ExcelUtil<CapPoolAccountRecord>(CapPoolAccountRecord.class);
        return util.exportExcel(list, "poolAccountRecord");
    }

    /**
     * 获取资金池记录详细信息
     */
    @ApiOperation("获取资金池记录详细信息")
    @PreAuthorize("@ss.hasPermi('capital:poolAccountRecord:query')")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(capPoolAccountRecordService.selectCapPoolAccountRecordById(id));
    }

    /**
     * 新增资金池记录
     */
    @ApiOperation("新增资金池记录")
    @PreAuthorize("@ss.hasPermi('capital:poolAccountRecord:add')")
    @Log(title = "资金池记录", businessType = BusinessType.INSERT)
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult add(@RequestBody CapPoolAccountRecord capPoolAccountRecord) {
        return toAjax(capPoolAccountRecordService.insertCapPoolAccountRecord(capPoolAccountRecord));
    }

    /**
     * 修改资金池记录
     */
    @ApiOperation("修改资金池记录")
    @PreAuthorize("@ss.hasPermi('capital:poolAccountRecord:edit')")
    @Log(title = "资金池记录", businessType = BusinessType.UPDATE)
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult edit(@RequestBody CapPoolAccountRecord capPoolAccountRecord) {
        return toAjax(capPoolAccountRecordService.updateCapPoolAccountRecord(capPoolAccountRecord));
    }

    /**
     * 删除资金池记录
     */
    @ApiOperation("删除资金池记录")
    @PreAuthorize("@ss.hasPermi('capital:poolAccountRecord:remove')")
    @Log(title = "资金池记录", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(capPoolAccountRecordService.deleteCapPoolAccountRecordByIds(ids));
    }

    @ApiOperation("通过资金类型查询")
    @PostMapping("selectByAmountType")
    public AjaxResult selectByAmountType(@RequestBody CapPoolAccountRecord capPoolAccountRecord) {
        return AjaxResult.success(capPoolAccountRecordService.selectByFromUserIdAndAmountType(capPoolAccountRecord));
    }


    @Autowired
    private ICapPoolAccountService capPoolAccountService;



    @ApiOperation("资金池首页查询")
    @PostMapping("capital_pool_home")
    public AjaxResult capital_pool_home(@RequestBody CapPoolAccountRecord capAccountRecord) {

        List<CapPoolAccountRecord> capPoolAccountRecordList = capPoolAccountRecordService.selectCapPoolAccountRecordList(capAccountRecord);
        HashMap<Object, Object> map = new HashMap<>();
        Double zong = 0.0;
        Double yong = 0.0;
        Double li = 0.0;
        Double yu = 0.0;
        if (capPoolAccountRecordList.size() > 0) {
            for (CapPoolAccountRecord capPoolAccountRecord : capPoolAccountRecordList) {
                zong += Double.valueOf(capPoolAccountRecord.getAmount().toString());
                if (capPoolAccountRecord.getAmountType().equals("1")) {
                    yong += Double.valueOf(capPoolAccountRecord.getAmount().toString());
                }
                if (capPoolAccountRecord.getAmountType().equals("2")) {
                    li += Double.valueOf(capPoolAccountRecord.getAmount().toString());
                }

            }

            CapPoolAccount capPoolAccount = new CapPoolAccount();
            capPoolAccount.setUserId(capAccountRecord.getFromUserId());

            List<CapPoolAccount> capPoolAccounts = capPoolAccountService.selectCapPoolAccountList(capPoolAccount);
            if (capPoolAccounts.size() > 0) {
                yu = Double.valueOf(capPoolAccount.getAmount().toString());
            }

        }

        map.put("zong", zong);
        map.put("yong", yong);
        map.put("li", li);
        map.put("yu", yu);
        map.put("xiangqing", capPoolAccountRecordList);
        return AjaxResult.success(map);
    }

}
